package com.example.foo.order.dao;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.order.model.Order;
import com.example.foo.order.query.OrderQuery;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@org.apache.ibatis.annotations.Mapper
public interface OrderDao extends com.example.core.dao.BaseDao<Order, Long> {

	public default PageResult<Order> findOrders(OrderQuery orderQuery, Pageable pageable) {
		QueryWrapper<Order> queryWrapper = createQueryWithBaseIf(orderQuery);
		//
		if (StringUtils.isNotEmpty(orderQuery.getOutTradeNo())) {
			queryWrapper.like("out_trade_no", orderQuery.getOutTradeNo());
		}
		if (StringUtils.isNotEmpty(orderQuery.getGoodsType())) {
			queryWrapper.eq("goods_type", orderQuery.getGoodsType());
		}
		if (orderQuery.getPayStatus() != null) {
			queryWrapper.eq("pay_status", orderQuery.getPayStatus());
		}
		if (StringUtils.isNotEmpty(orderQuery.getPayType())) {
			queryWrapper.eq("pay_type", orderQuery.getPayType());
		}
		if (orderQuery.getStatus() != null) {
			queryWrapper.eq("status", orderQuery.getStatus());
		}
		if (orderQuery.getHandlingMethod() != null) {
			queryWrapper.eq("handling_method", orderQuery.getHandlingMethod());
		}
		if (StringUtils.isNotEmpty(orderQuery.getCardSn())) {
			queryWrapper.like("card_sn", orderQuery.getCardSn());
		}
		if (orderQuery.getUid() != null) {
			queryWrapper.eq("uid", orderQuery.getUid());
		}
		if (StringUtils.isNotEmpty(orderQuery.getRealName())) {
			queryWrapper.like("real_name", orderQuery.getRealName());
		}
		if (StringUtils.isNotEmpty(orderQuery.getUserPhone())) {
			queryWrapper.like("user_phone", orderQuery.getUserPhone());
		}
		if (StringUtils.isNotEmpty(orderQuery.getGoodsTypeList())) {
			queryWrapper.in("goods_type", Arrays.asList(orderQuery.getGoodsTypeList().split(",")));
		}
		if (StringUtils.isNotEmpty(orderQuery.getAppId())) {
			queryWrapper.eq("app_id", orderQuery.getAppId());
		}
		if (StringUtils.isNotEmpty(orderQuery.getAppIdList())) {
			queryWrapper.in("app_id", Arrays.asList(orderQuery.getAppIdList().split(",")));
		}
		return findPages(queryWrapper, pageable);
	}

	@Select("SELECT * FROM orders where (goods_type = 'zj2w' or goods_type = 'zj3w') and pay_status = 1 and DATE_FORMAT(pay_time,'%Y-%m-%d') = #{date}")
	List<Order> findZjShareOrders(@Param("date") String date);

}
